#ifndef M_TIMESPEC_H
#define M_TIMESPEC_H

#include <time.h>
#include <sys/time.h>

static struct timespec ms2timespec(int msecs)
{
    long secs, add;
    struct timespec ts;

    clock_gettime(CLOCK_REALTIME, &ts);
    // printf("old clock: %lu, %lu\n", ts.tv_sec, ts.tv_nsec);
    secs = msecs / 1000;
    msecs = msecs % 1000;
    msecs = msecs * 1000 * 1000 + ts.tv_nsec;
    add = msecs / (1000 * 1000 * 1000);
    ts.tv_sec += (add + secs);
    ts.tv_nsec = msecs % (1000 * 1000 * 1000);
    // printf("new clock: %lu, %lu\n", ts.tv_sec, ts.tv_nsec);
    return ts;
}

#endif
